package cn.itcast.mq.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Calendar;

@Component
@Slf4j
public class SpringRabbitListener {

    @RabbitListener(queues = "dead.queue")
    public void listenSimpleQueue(String msg, Message message, Channel channel) {
        long tag = message.getMessageProperties().getDeliveryTag();
        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
        try {
            System.out.println(Calendar.getInstance().getTimeInMillis());
//            int i=1/0;
            Thread.sleep(3000);
            channel.basicAck(tag,false);
        } catch (Exception e) {
            try {
                channel.basicReject(tag,true);
            } catch (IOException ex) {
            }

        }

    }
}
